﻿<template>
  <a-card :bordered="false">
    <div class="table-operator">
      <a-button v-if="hasPerm('SO_SODetails.Add')" type="primary" icon="plus" @click="handleAdd()">新建</a-button>
      <a-button
        v-if="hasPerm('SO_SODetails.Delete')"
        type="primary"
        icon="minus"
        @click="handleDelete(selectedRowKeys)"
        :disabled="!hasSelected()"
        :loading="loading"
      >删除</a-button>
      <a-button type="primary" icon="redo" @click="getDataList()">刷新</a-button>
    </div>

    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="4" :sm="24">
            <a-form-item label="查询类别">
              <a-select v-model="queryParam.condition">
		                <a-select-option key="AutoID">销售订单子表标识</a-select-option>
                <a-select-option key="cSOCode">销售订单号</a-select-option>
                <a-select-option key="cInvCode">存货编码</a-select-option>
                <a-select-option key="cMemo">备注 </a-select-option>
                <a-select-option key="cFree1">存货自由项1</a-select-option>
                <a-select-option key="cFree2">存货自由项2</a-select-option>
                <a-select-option key="iSOsID">销售订单子表标识2</a-select-option>
                <a-select-option key="cInvName">存货名称</a-select-option>
                <a-select-option key="cDefine22">表体自定义项22</a-select-option>
                <a-select-option key="cDefine23">表体自定义项23</a-select-option>
                <a-select-option key="cDefine24">表体自定义项24</a-select-option>
                <a-select-option key="cDefine25">表体自定义项25</a-select-option>
                <a-select-option key="cItemCode">项目编码</a-select-option>
                <a-select-option key="cItem_class">项目大类编码</a-select-option>
                <a-select-option key="cItemName">项目名称</a-select-option>
                <a-select-option key="cItem_CName">项目大类名称</a-select-option>
                <a-select-option key="cFree3">存货自由项3</a-select-option>
                <a-select-option key="cFree4">存货自由项4</a-select-option>
                <a-select-option key="cFree5">存货自由项5</a-select-option>
                <a-select-option key="cFree6">存货自由项6</a-select-option>
                <a-select-option key="cFree7">存货自由项7</a-select-option>
                <a-select-option key="cFree8">存货自由项8</a-select-option>
                <a-select-option key="cFree9">存货自由项9</a-select-option>
                <a-select-option key="cFree10">存货自由项10</a-select-option>
                <a-select-option key="cUnitID">计量单位编码</a-select-option>
                <a-select-option key="ID">销售订单主表标识</a-select-option>
                <a-select-option key="cDefine28">表体自定义项28</a-select-option>
                <a-select-option key="cDefine29">表体自定义项29</a-select-option>
                <a-select-option key="cDefine30">表体自定义项30</a-select-option>
                <a-select-option key="cDefine31">表体自定义项31</a-select-option>
                <a-select-option key="cDefine32">表体自定义项32</a-select-option>
                <a-select-option key="cDefine33">表体自定义项33</a-select-option>
                <a-select-option key="cQuoCode">报价单号</a-select-option>
                <a-select-option key="cSCloser">关闭标记</a-select-option>
                <a-select-option key="cContractID">合同号</a-select-option>
                <a-select-option key="cContractTagCode">合同标的</a-select-option>
                <a-select-option key="cContractRowGuid">合同cGuid</a-select-option>
                <a-select-option key="cCusInvCode">客户存货编码</a-select-option>
                <a-select-option key="cCusInvName">客户存货名称</a-select-option>
                <a-select-option key="cpreordercode">预订单号</a-select-option>
                <a-select-option key="cdemandcode">需求分类代号</a-select-option>
                <a-select-option key="cdemandmemo">需求分类说明</a-select-option>
                <a-select-option key="ccorvouchtype">来源单据类型</a-select-option>
                <a-select-option key="body_outid">body_outid</a-select-option>
                <a-select-option key="cdetailsdemandcode">子键需求分类代号</a-select-option>
                <a-select-option key="cdetailsdemandmemo">子键需求分类说明</a-select-option>
                <a-select-option key="cbSysBarCode">单据行条码</a-select-option>
                <a-select-option key="cParentCode">父节点编码</a-select-option>
                <a-select-option key="cChildCode">子节点编码</a-select-option>
                <a-select-option key="cFactoryCode">cFactoryCode</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="24">
            <a-form-item>
              <a-input allowClear @keydown="inputKeyDown" v-model="queryParam.keyword" placeholder="关键字" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button type="primary" @click="() => {this.pagination.current = 1; this.getDataList()}">查询</a-button>
            <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      :scroll="{ y: tableHight }"
      :columns="columns"
      :rowKey="(row, index) => row.Id"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: 'checkbox' }"
      :bordered="true"
      size="small"
      :customRow="loadRow">
      <span slot="CreateTime" slot-scope="CreateTime">{{ timeFormat(CreateTime,'YYYY-MM-DD HH:mm') }}</span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a v-if="hasPerm('SO_SODetails.Edit')" @click="handleEdit(record.Id)">编辑</a>
          <a-divider v-if="hasPerm('SO_SODetails.Edit')&&hasPerm('SO_SODetails.Delete')" type="vertical" />
          <a v-if="hasPerm('SO_SODetails.Delete')" @click="handleDelete([record.Id])">删除</a>
        </template>
      </span>
    </a-table>
    <edit-form ref="editForm" :parentObj="this" :PDeptId="this.DeptId" @returnMain="handleReturnMain"></edit-form>
  </a-card>
</template>

<script>
import { mapState } from 'vuex'
import EditForm from './EditForm'
const columns = [
  { title: '销售订单子表标识', dataIndex: 'AutoID', align: 'center', width: '10%' },
  { title: '销售订单号', dataIndex: 'cSOCode', align: 'center', width: '10%' },
  { title: '存货编码', dataIndex: 'cInvCode', align: 'center', width: '10%' },
  { title: '预发货日期', dataIndex: 'dPreDate', align: 'center', width: '10%' },
  { title: '数量', dataIndex: 'iQuantity', align: 'center', width: '10%' },
  { title: '辅计量数量', dataIndex: 'iNum', align: 'center', width: '10%' },
  { title: '报价', dataIndex: 'iQuotedPrice', align: 'center', width: '10%' },
  { title: '原币无税单价', dataIndex: 'iUnitPrice', align: 'center', width: '10%' },
  { title: '原币含税单价', dataIndex: 'iTaxUnitPrice', align: 'center', width: '10%' },
  { title: '原币无税金额', dataIndex: 'iMoney', align: 'center', width: '10%' },
  { title: '原币税额', dataIndex: 'iTax', align: 'center', width: '10%' },
  { title: '原币价税合计', dataIndex: 'iSum', align: 'center', width: '10%' },
  { title: '原币折扣额', dataIndex: 'iDisCount', align: 'center', width: '10%' },
  { title: '本币无税单价', dataIndex: 'iNatUnitPrice', align: 'center', width: '10%' },
  { title: '本币无税金额', dataIndex: 'iNatMoney', align: 'center', width: '10%' },
  { title: '本币税额', dataIndex: 'iNatTax', align: 'center', width: '10%' },
  { title: '本币价税合计', dataIndex: 'iNatSum', align: 'center', width: '10%' },
  { title: '本币折扣额', dataIndex: 'iNatDisCount', align: 'center', width: '10%' },
  { title: '累计发货辅计量数量', dataIndex: 'iFHNum', align: 'center', width: '10%' },
  { title: '累计发货数量', dataIndex: 'iFHQuantity', align: 'center', width: '10%' },
  { title: '累计原币发货金额', dataIndex: 'iFHMoney', align: 'center', width: '10%' },
  { title: '累计开票数量', dataIndex: 'iKPQuantity', align: 'center', width: '10%' },
  { title: '累计开票辅计量数量', dataIndex: 'iKPNum', align: 'center', width: '10%' },
  { title: '累计原币开票金额', dataIndex: 'iKPMoney', align: 'center', width: '10%' },
  { title: '备注 ', dataIndex: 'cMemo', align: 'center', width: '10%' },
  { title: '存货自由项1', dataIndex: 'cFree1', align: 'center', width: '10%' },
  { title: '存货自由项2', dataIndex: 'cFree2', align: 'center', width: '10%' },
  { title: '现无用 ', dataIndex: 'bFH', align: 'center', width: '10%' },
  { title: '销售订单子表标识2', dataIndex: 'iSOsID', align: 'center', width: '10%' },
  { title: '扣率', dataIndex: 'KL', align: 'center', width: '10%' },
  { title: '二次扣率', dataIndex: 'KL2', align: 'center', width: '10%' },
  { title: '存货名称', dataIndex: 'cInvName', align: 'center', width: '10%' },
  { title: '税率', dataIndex: 'iTaxRate', align: 'center', width: '10%' },
  { title: '表体自定义项22', dataIndex: 'cDefine22', align: 'center', width: '10%' },
  { title: '表体自定义项23', dataIndex: 'cDefine23', align: 'center', width: '10%' },
  { title: '表体自定义项24', dataIndex: 'cDefine24', align: 'center', width: '10%' },
  { title: '表体自定义项25', dataIndex: 'cDefine25', align: 'center', width: '10%' },
  { title: '表体自定义项26', dataIndex: 'cDefine26', align: 'center', width: '10%' },
  { title: '表体自定义项27', dataIndex: 'cDefine27', align: 'center', width: '10%' },
  { title: '项目编码', dataIndex: 'cItemCode', align: 'center', width: '10%' },
  { title: '项目大类编码', dataIndex: 'cItem_class', align: 'center', width: '10%' },
  { title: '项目名称', dataIndex: 'cItemName', align: 'center', width: '10%' },
  { title: '项目大类名称', dataIndex: 'cItem_CName', align: 'center', width: '10%' },
  { title: '存货自由项3', dataIndex: 'cFree3', align: 'center', width: '10%' },
  { title: '存货自由项4', dataIndex: 'cFree4', align: 'center', width: '10%' },
  { title: '存货自由项5', dataIndex: 'cFree5', align: 'center', width: '10%' },
  { title: '存货自由项6', dataIndex: 'cFree6', align: 'center', width: '10%' },
  { title: '存货自由项7', dataIndex: 'cFree7', align: 'center', width: '10%' },
  { title: '存货自由项8', dataIndex: 'cFree8', align: 'center', width: '10%' },
  { title: '存货自由项9', dataIndex: 'cFree9', align: 'center', width: '10%' },
  { title: '存货自由项10', dataIndex: 'cFree10', align: 'center', width: '10%' },
  { title: '换算率', dataIndex: 'iInvExchRate', align: 'center', width: '10%' },
  { title: '计量单位编码', dataIndex: 'cUnitID', align: 'center', width: '10%' },
  { title: '销售订单主表标识', dataIndex: 'ID', align: 'center', width: '10%' },
  { title: '表体自定义项28', dataIndex: 'cDefine28', align: 'center', width: '10%' },
  { title: '表体自定义项29', dataIndex: 'cDefine29', align: 'center', width: '10%' },
  { title: '表体自定义项30', dataIndex: 'cDefine30', align: 'center', width: '10%' },
  { title: '表体自定义项31', dataIndex: 'cDefine31', align: 'center', width: '10%' },
  { title: '表体自定义项32', dataIndex: 'cDefine32', align: 'center', width: '10%' },
  { title: '表体自定义项33', dataIndex: 'cDefine33', align: 'center', width: '10%' },
  { title: '表体自定义项34', dataIndex: 'cDefine34', align: 'center', width: '10%' },
  { title: '表体自定义项35', dataIndex: 'cDefine35', align: 'center', width: '10%' },
  { title: '表体自定义项36', dataIndex: 'cDefine36', align: 'center', width: '10%' },
  { title: '表体自定义项37', dataIndex: 'cDefine37', align: 'center', width: '10%' },
  { title: '采购数量', dataIndex: 'FPurQuan', align: 'center', width: '10%' },
  { title: '零售单价', dataIndex: 'fSaleCost', align: 'center', width: '10%' },
  { title: '零售金额', dataIndex: 'fSalePrice', align: 'center', width: '10%' },
  { title: '报价单号', dataIndex: 'cQuoCode', align: 'center', width: '10%' },
  { title: '报价单id', dataIndex: 'iQuoID', align: 'center', width: '10%' },
  { title: '关闭标记', dataIndex: 'cSCloser', align: 'center', width: '10%' },
  { title: '预完工日期', dataIndex: 'dPreMoDate', align: 'center', width: '10%' },
  { title: '行号', dataIndex: 'iRowNo', align: 'center', width: '10%' },
  { title: '客户BOMID', dataIndex: 'iCusBomID', align: 'center', width: '10%' },
  { title: '下达生产数量', dataIndex: 'iMoQuantity', align: 'center', width: '10%' },
  { title: '合同号', dataIndex: 'cContractID', align: 'center', width: '10%' },
  { title: '合同标的', dataIndex: 'cContractTagCode', align: 'center', width: '10%' },
  { title: '合同cGuid', dataIndex: 'cContractRowGuid', align: 'center', width: '10%' },
  { title: 'PTO母件顺序号', dataIndex: 'iPPartSeqID', align: 'center', width: '10%' },
  { title: '母件物料id', dataIndex: 'iPPartID', align: 'center', width: '10%' },
  { title: '母件数量', dataIndex: 'iPPartQty', align: 'center', width: '10%' },
  { title: '客户存货编码', dataIndex: 'cCusInvCode', align: 'center', width: '10%' },
  { title: '客户存货名称', dataIndex: 'cCusInvName', align: 'center', width: '10%' },
  { title: '预留数量', dataIndex: 'iPreKeepQuantity', align: 'center', width: '10%' },
  { title: '预留件数', dataIndex: 'iPreKeepNum', align: 'center', width: '10%' },
  { title: '预留总数量', dataIndex: 'iPreKeepTotQuantity', align: 'center', width: '10%' },
  { title: '预留总件数', dataIndex: 'iPreKeepTotNum', align: 'center', width: '10%' },
  { title: '释放日期', dataIndex: 'dreleasedate', align: 'center', width: '10%' },
  { title: '客户最低售价', dataIndex: 'fcusminprice', align: 'center', width: '10%' },
  { title: '进口数量', dataIndex: 'fimquantity', align: 'center', width: '10%' },
  { title: '委外数量', dataIndex: 'fomquantity', align: 'center', width: '10%' },
  { title: '是否全部采购', dataIndex: 'ballpurchase', align: 'center', width: '10%' },
  { title: '入库数量', dataIndex: 'finquantity', align: 'center', width: '10%' },
  { title: '成本数量', dataIndex: 'icostquantity', align: 'center', width: '10%' },
  { title: '成本金额', dataIndex: 'icostsum', align: 'center', width: '10%' },
  { title: '出库数量', dataIndex: 'foutquantity', align: 'center', width: '10%' },
  { title: '出库件数', dataIndex: 'foutnum', align: 'center', width: '10%' },
  { title: '累计收款原币', dataIndex: 'iexchsum', align: 'center', width: '10%' },
  { title: '累计收款本币', dataIndex: 'imoneysum', align: 'center', width: '10%' },
  { title: '无', dataIndex: 'dufts', align: 'center', width: '10%' },
  { title: '预订单autoid', dataIndex: 'iaoids', align: 'center', width: '10%' },
  { title: '预订单号', dataIndex: 'cpreordercode', align: 'center', width: '10%' },
  { title: '退货数量', dataIndex: 'fretquantity', align: 'center', width: '10%' },
  { title: '退货件数', dataIndex: 'fretnum', align: 'center', width: '10%' },
  { title: '关闭日期', dataIndex: 'dbclosedate', align: 'center', width: '10%' },
  { title: '关闭时间', dataIndex: 'dbclosesystime', align: 'center', width: '10%' },
  { title: '是否订单BOM', dataIndex: 'bOrderBOM', align: 'center', width: '10%' },
  { title: '订单BOM是否完成', dataIndex: 'bOrderBOMOver', align: 'center', width: '10%' },
  { title: '需求跟踪方式（1-销售订单行号；4-需求分类号5-销售订单号）', dataIndex: 'idemandtype', align: 'center', width: '10%' },
  { title: '需求分类代号', dataIndex: 'cdemandcode', align: 'center', width: '10%' },
  { title: '需求分类说明', dataIndex: 'cdemandmemo', align: 'center', width: '10%' },
  { title: '采购金额', dataIndex: 'fPurSum', align: 'center', width: '10%' },
  { title: '采购发票数量', dataIndex: 'fPurBillQty', align: 'center', width: '10%' },
  { title: '采购发票金额', dataIndex: 'fPurBillSum', align: 'center', width: '10%' },
  { title: '进口订单明细行', dataIndex: 'iimid', align: 'center', width: '10%' },
  { title: '来源单据类型', dataIndex: 'ccorvouchtype', align: 'center', width: '10%' },
  { title: '来源单据行号', dataIndex: 'icorrowno', align: 'center', width: '10%' },
  { title: '使用客户BOM', dataIndex: 'busecusbom', align: 'center', width: '10%' },
  { title: 'body_outid', dataIndex: 'body_outid', align: 'center', width: '10%' },
  { title: '已审核订货数量', dataIndex: 'fVeriDispQty', align: 'center', width: '10%' },
  { title: '已审核订货原币金额', dataIndex: 'fVeriDispSum', align: 'center', width: '10%' },
  { title: '报价含税标识', dataIndex: 'bsaleprice', align: 'center', width: '10%' },
  { title: '赠品标识', dataIndex: 'bgift', align: 'center', width: '10%' },
  { title: '产品预测单子表编码', dataIndex: 'forecastdid', align: 'center', width: '10%' },
  { title: '子键需求分类代号', dataIndex: 'cdetailsdemandcode', align: 'center', width: '10%' },
  { title: '子键需求分类说明', dataIndex: 'cdetailsdemandmemo', align: 'center', width: '10%' },
  { title: '库存调拨数量', dataIndex: 'fTransedQty', align: 'center', width: '10%' },
  { title: '单据行条码', dataIndex: 'cbSysBarCode', align: 'center', width: '10%' },
  { title: '请购数量', dataIndex: 'fappqty', align: 'center', width: '10%' },
  { title: '父节点编码', dataIndex: 'cParentCode', align: 'center', width: '10%' },
  { title: '子节点编码', dataIndex: 'cChildCode', align: 'center', width: '10%' },
  { title: '使用数量', dataIndex: 'fchildqty', align: 'center', width: '10%' },
  { title: '权重比例', dataIndex: 'fchildrate', align: 'center', width: '10%' },
  { title: '发货模式', dataIndex: 'iCalcType', align: 'center', width: '10%' },
  { title: '无', dataIndex: 'iCurPartid', align: 'center', width: '10%' },
  { title: 'cFactoryCode', dataIndex: 'cFactoryCode', align: 'center', width: '10%' },
  { title: 'GCSourceId', dataIndex: 'GCSourceId', align: 'center', width: '10%' },
  { title: 'GCSourceIds', dataIndex: 'GCSourceIds', align: 'center', width: '10%' },
  { title: '创建时间', dataIndex: 'CreateTime', width: '10%', align: 'center', scopedSlots: { customRender: 'CreateTime' } },
  { title: '操作', dataIndex: 'action', align: 'left', scopedSlots: { customRender: 'action' } }
]

export default {
  components: {
    EditForm
  },
  computed: {
    ...mapState({
      DeptId: state => state.app.DeptId
    })
  },
  watch: {
    DeptId: {
      handler(n, o) {
        this.$nextTick(() => {
          // 页面渲染完成后的回调
          this.tableHight = this.getTableHeight(true)
        })
        this.getDataList()
      },
      immediate: true
    }
  },
  mounted () {
    this.$nextTick(() => {
      // 页面渲染完成后的回调
      this.tableHight = this.getTableHeight(true) + 24.5
    })
    this.getDataList()
  },
  data () {
    return {
      tableHight: 570, // 表格高度
      data: [],
      pagination: {
        current: 1,
        pageSize: 20,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['20', '30', '40', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
        showSizeChanger: true
      },
      filters: {},
      sorter: { field: 'Id', order: 'desc' },
      loading: false,
      columns,
      queryParam: {},
      selectedRows: [],
      selectedRowKeys: []
    }
  },
  methods: {
    handleTableChange (pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.sorter = { ...sorter }
      this.getDataList()
    },
    getDataList () {
      this.selectedRowKeys = []
      this.loading = true
      this.$http
        .post('/OrderManagement/SO_SODetails/GetDataList', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order || 'desc',
	  DeptId: this.DeptId,
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
    inputKeyDown (e) {
      if (e.keyCode === 13) {
        if (!this.queryParam[0].keyword) return this.$message.error('请输入查询条件')
        this.getDataList()
      }
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    hasSelected () {
      return this.selectedRowKeys.length > 0
    },
    handleAdd () {
      this.$refs.editForm.openForm(null, '新建销售订单子表信息')
    },
    handleEdit (id) {
      this.$refs.editForm.openForm(id, '编辑销售订单子表信息')
    },
    handleDelete (ids) {
      var thisObj = this
      this.$confirm({
        title: '确认删除吗?',
        onOk () {
          return new Promise((resolve, reject) => {
            thisObj.$http.post('/OrderManagement/SO_SODetails/DeleteData', ids).then(resJson => {
              resolve()

              if (resJson.Success) {
                thisObj.$message.success('操作成功!')

                thisObj.getDataList()
              } else {
                thisObj.$message.error(resJson.Msg)
              }
            })
          })
        }
      })
    },
    handleReturnMain () {
      this.getDataList()
    },
    // 表格点击选中取消
    loadRow (record, index) {
      return {
        on: {
          click: () => {
            // 记录为disabled则直接返回，默认为不可选
            if (record.disabled) return
            const type = 'checkbox'
            const key = record.Id
            let selectedRows = this.selectedRows
            let selectedRowKeys = this.selectedRowKeys
            if (type === 'radio') {
              selectedRowKeys = [key]
              selectedRows = [record]
            }
            if (!this.selectedRowKeys.includes(key)) {
              selectedRowKeys.push(key)
              selectedRows.push(record)
            } else {
              const index = this.selectedRowKeys.findIndex(skey => skey === key)
              selectedRows.splice(index, 1)
              selectedRowKeys.splice(index, 1)
            }
            this.updateSelect(selectedRowKeys, selectedRows)
            this.onSelectChange(selectedRowKeys, selectedRows)
          }
        }
      }
    },
    // 更新选中操作
    updateSelect (selectedRowKeys, selectedRows) {
      this.selectedRows = selectedRows
      this.selectedRowKeys = selectedRowKeys
    }
  }
}
</script>
